home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-05 | 3.1 KB | 121 lines | [MATS/MATL] |
- echo off;
- % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
- % To accompany the text:
- % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
- % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
- % This free software is complements of the author.
-
- % Algorithm 5.4 (Cubic Splines).
- % Section 5.3, Interpolation by Spline Functions, Page 297
- echo on; clc; format short; hold off; clear
-
- % This program finds the cubic spline interpolant.
-
- % Given a set of data points
-
- % { (x , y ), (x , y ) ,..., (x , y ) }.
- % 1 1 2 2 n n
-
- % The abscissas and ordinates are stored in X and Y, respectively.
-
- % X = [x , x ,..., x ]; Y = [y , y ,..., y ];
- % 1 2 n 1 2 n
-
- % Remark. csfit.m and cs.m are used for Algorithm 5.4
-
- pause % Press any key to continue.
-
- clc;
- % Place the abscissas for the points in X.
-
- % Place the ordinates for the points in Y.
-
- X = [0 1 3 4 6 ];
-
- Y = [0 0.5 2.0 1.5 2.5];
-
- X = [0 1 2 3 ];
- Y = [0 0.5 2.0 1.5];
-
- pause % Press any key to graph data points.
-
- clc; clg;
- a = min(X)-0.2; b = max(X)+0.2;
- c = min(Y)-0.2; d = max(Y)+0.2;
- axis([a b c d]);...
- plot(X,Y,'or');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- xlabel('x');...
- ylabel('y');...
- title('The given x-y data points');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- points = [X;Y];
- format short;
- clc,disp('The given x-y points:'),...
- disp(' x y'),disp(points')
-
- pause % Press any key for the menu of spline curves.
-
- clc;
- Mx1=' Available curves are:';
- Mx2='(1) Clamped Spline';
- Mx3='(2) Natural Spline';
- Mx4='(3) Extrapolated Spline (Cubic runout spline)';
- Mx5='(4) Parabolically Terminated Spline';
- Mx6='(5) Endpoint Curvature Adjusted Spline';
- clc,disp(''),disp(Mx1),disp(''),disp(Mx2),disp(''),disp(Mx3),,...
- disp(''),disp(Mx4),disp(''),disp(Mx5),disp(''),disp(Mx6),...
- ct = input('Enter the spline type <1-5> ');...
- if length(ct)==0, ct=2; end;...
- disp('');...
- S = csfit(X,Y,ct);
-
- pause % Press any key to fit the spline curve.
-
- clc;
- hs = (max(X)-min(X))/200;
- Xs = min(X):hs:max(X);
- Ys = cs(S,X,Xs);
- axis([a b c d]);...
- plot(X,Y,'or',Xs,Ys,'-g');...
- hold on;...
- plot([a b],[0 0],'b',[0 0],[c d],'b');...
- xlabel('x');...
- ylabel('y');...
- title('The cubic spline: y = S(x)');...
- grid;...
- axis;...
- hold off;...
- shg; pause % Press any key to continue.
-
- Mx1 ='The given x-y points:';
- Mx2 = ' x y';
- if ct==1,Mx='clamped spline';end
- if ct==2,Mx='natural spline';end
- if ct==3,Mx='extrapolated spline (cubic runout spline)';end
- if ct==4,Mx='parabolically terminated spline';end
- if ct==5,Mx='endpoint curvature adjusted spline';end
- Mx3 = ['The ',Mx,' was determined.'];
- Mx4 = 'The spline coefficients are:';
- clc,echo off,diary output,...
- disp(Mx1),disp(Mx2),disp([X;Y]'),...
- disp(Mx3),disp(Mx4),disp(''),...
- [n1 n2] = size(S);...
- for k = 1:n1,
- Mx5 = ['S',num2str(k),'(x):'];
- disp(Mx5);disp(S(k,4:-1:1));
- end,diary off,echo on
-
- pause % Press any key to continue.
-
- points = [X;Y;CS(S,X,X);Y-CS(S,X,X)]';
- Mx5 = ' x(k) y(k) S(x(k)) error';
- clc,echo off,diary output,...
- disp(''),disp(Mx5),disp(points),diary off,echo on
-
-